package com.yuanjin.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.yuanjin.R;
import com.yuanjin.adapter.ViewPagerAdapter;

public class Guide extends Activity implements OnPageChangeListener {
	private ViewPager vp;
	// 新建list来装载切换的页面
	private List<View> views;
	private ViewPagerAdapter vpAdapter;
	private int[] point_ids = { R.id.iv1_point, R.id.iv2_point, R.id.iv3_point };
	private Button intobtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide);
		initView();
	}

	// 视图初始化
	private void initView() {
		// 获取页面布局填充器
		LayoutInflater inflater = LayoutInflater.from(this);
		// 初始化List
		views = new ArrayList<View>();
		// 往list中添加引导页面
		views.add(inflater.inflate(R.layout.guide_one, null));
		views.add(inflater.inflate(R.layout.guide_two, null));
		views.add(inflater.inflate(R.layout.guide_three, null));

		// 初始化viewPager适配器
		vpAdapter = new ViewPagerAdapter(views, this);
		// 通过id找到viewpager
		vp = (ViewPager) findViewById(R.id.guide_viewpager);
		vp.setAdapter(vpAdapter);
		// vp实现onPageChangeListener监听
		vp.setOnPageChangeListener(this);
		
		//设置进入按钮
		intobtn = (Button) findViewById(R.id.guide_intoBtn);
		//实现点击监听
		intobtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//跳转到主页面
				Intent intent=new Intent(Guide.this,LoginPage.class);
				startActivity(intent);
				//结束当前页面
				finish();
			}
		});

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// arg0为当前页面的标号
		ImageView iv;
		
		for (int i = 0; i < point_ids.length; i++) {
			// 获取页面的对应点图片,然后设置该图片的显示样式资源src
			iv = (ImageView) findViewById(point_ids[i]);
			if (i == arg0) {
				iv.setImageResource(R.drawable.guide_point_selected_blue2);
			} else {
				iv.setImageResource(R.drawable.guide_point);
			}
		}
		// 若进入最后一个引导页面，则显示进入按钮
		if (arg0==point_ids.length-1) {
			intobtn.setVisibility(View.VISIBLE);
		}else {
			intobtn.setVisibility(View.GONE);
		}
	}
}
